package com.maixin.code.action;

import java.util.ArrayList;
import java.util.List;

import com.maixin.code.base.BaseAction;
import com.maixin.code.entity.UserEnter;
import com.maixin.code.entity.UserSalaryInfo;
import com.maixin.code.service.UserEnterService;
import com.maixin.code.service.UserSalaryInfoService;
import com.maixin.code.utils.DateUtils;
import com.maixin.code.utils.Json2JavaUtils;

public class UserEnterAction  extends BaseAction{
	
	private static final long serialVersionUID = -2657616945344890641L;
	private UserEnterService userEnterSer;
	private UserSalaryInfoService userSalarySer;
	private List<UserEnter> list;
	private UserSalaryInfo userSalaryInfo;
	private UserEnter userEnter;
	private Integer cnt;
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findUserEnter() throws Exception{
		list = userEnterSer.getBaseDao().findAll(UserEnter.class, start, limit);	
		cnt = userEnterSer.getBaseDao().findTabCnt(UserEnter.class);
		return SUCCESS;
	}

	/**
	 * 显示用户详情
	 * @return
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	public String showUserDetail() throws NumberFormatException, Exception{
			List<UserSalaryInfo> userList = userSalarySer.getBaseDao().findByPropertes(
					new String [] {"userEnterId"}, new Integer[]{Integer.parseInt(userEnter.getUserId())}, UserSalaryInfo.class);
			if(userList == null ||userList.size() == 0){
				//userSalaryInfo = null;
			}else{
				userSalaryInfo = userList.get(0);
			}
		return SUCCESS;
	}
	
	/**
	 * 添加或修改用户
	 * @return
	 */
	public String addUserEnterAndSalary(){
		try{
			List<?>  lUserEnter = Json2JavaUtils.json2Java(content, UserEnter.class);
			List<?>  lUserSalaryInfo = Json2JavaUtils.json2Java(content, UserSalaryInfo.class);
			String[] arr = content.split(",");
			
			list = new ArrayList<UserEnter>();
			for (int i = 0; i < lUserEnter.size(); i++) {
				UserEnter u_list = (UserEnter) lUserEnter.get(i);
				UserSalaryInfo s_list = (UserSalaryInfo) lUserSalaryInfo.get(i);
				if(arr[20].split(":")[1].equals("\"\"")){
					s_list.setLeaveDate(null);
				}else{
					s_list.setLeaveDate(DateUtils.str2Date(arr[20].split(":")[1].replaceAll("\"", "")));
				}
				if(null == u_list.getId() || "".equals(u_list.getId())){
					userEnter = userEnterSer.getBaseDao().save(u_list);
					s_list.setUserEnterId(userEnter.getId());
					userSalaryInfo = userSalarySer.getBaseDao().save(s_list);
				}else{
					userEnter = userEnterSer.getBaseDao().update(u_list);
					s_list.setUserEnterId(userEnter.getId());
					userSalaryInfo = userSalarySer.getBaseDao().update(s_list);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String delUserEnterAndSalary() throws Exception{
		userEnterSer.getBaseDao().delete(userEnter);
		userSalaryInfo = userSalarySer.getBaseDao().findByPropertes(new String []{"userEnterId"}, new Integer[]{userSalaryInfo.getUserEnterId()}, UserSalaryInfo.class).get(0);
		userSalarySer.getBaseDao().delete(userSalaryInfo);
		
		return SUCCESS;
	}
	
	
	public UserEnterService getUserEnterSer() {
		return userEnterSer;
	}

	public void setUserEnterSer(UserEnterService userEnterSer) {
		this.userEnterSer = userEnterSer;
	}

	public UserSalaryInfoService getUserSalarySer() {
		return userSalarySer;
	}

	public void setUserSalarySer(UserSalaryInfoService userSalarySer) {
		this.userSalarySer = userSalarySer;
	}

	public List<UserEnter> getList() {
		return list;
	}

	public void setList(List<UserEnter> list) {
		this.list = list;
	}

	public UserSalaryInfo getUserSalaryInfo() {
		return userSalaryInfo;
	}

	public void setUserSalaryInfo(UserSalaryInfo userSalaryInfo) {
		this.userSalaryInfo = userSalaryInfo;
	}

	public UserEnter getUserEnter() {
		return userEnter;
	}

	public void setUserEnter(UserEnter userEnter) {
		this.userEnter = userEnter;
	}
	public Integer getCnt() {
		return cnt;
	}
	public void setCnt(Integer cnt) {
		this.cnt = cnt;
	}
}
